package com.gmall.seckill.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gmall.model.seckill.SeckillGoods;
import com.gmall.seckill.mapper.SecKillGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

@Service
public class SecKillGoodsService {

    @Autowired
    SecKillGoodsMapper secKillGoodsMapper;

    /**
     * 操作数据库
     * @param days
     * @return
     */
    public List<SeckillGoods> findSecKillGoodsListAfter(Integer days){

        //开始时间  结束时间
        //结束时间 > now() and 开始时间 < now() + 7day
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime daysAfterDays = localDateTime.plusDays(days);
        //帮我写出LocalDateTime 转化为 Date java代码
        ZoneOffset zoneOffset = ZoneOffset.ofHours(8);//东八区
        ZoneId zoneId = ZoneOffset.systemDefault();//系统缺省时区
        Date now = Date.from(localDateTime.atZone(zoneId).toInstant());
        Date sevenDaysLater = Date.from(daysAfterDays.atZone(zoneId).toInstant());
        QueryWrapper<SeckillGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().ge(SeckillGoods::getEndTime,now);
        queryWrapper.lambda().le(SeckillGoods::getStartTime,sevenDaysLater);

        //查询数据库 SQL语句
        return secKillGoodsMapper.selectList(queryWrapper);
    }
}
